package club.xinghe.servlet.teacher;

import club.xinghe.util.JsonServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/teacher/UpEServlet")
public class UpEServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String estring = request.getParameter("estring");
        int tidy = Integer.parseInt(request.getParameter("tidy"));
        int tidz = Integer.parseInt(request.getParameter("tidz"));
        String[] tkcs = estring.split(",");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < tkcs.length ; i++) {
            list.add(tkcs[i]);
        }

        if (tidy==0){
            int n = 0;
            for (int i = 0; i < list.size() ; i++) {
                if (Integer.parseInt(list.get(i))==tidz){
                    n++;
                }
            }
            if (n==0){
                list.add(String.valueOf(tidz));
            }
        }

        if (tidz==0){
            list.remove(String.valueOf(tidy));
        }

        String estring1 = "";
        if (list.size()>0){
            for (String tt:list
            ) {
                estring1 += tt +",";
            }
        }
        JsonServlet.getJson(request,response,estring1.substring(0,estring1.length()-1));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
